/**
 * @file util
 * @description useSpecAttr
 * @author fanxueliang<fanxueliang
 */

import {keyReg, formatKey} from '../utils';
import {isUndefined} from 'lodash-es';

export type ShallowObjectType<T> = Record<string, T>;

export function setNestedObject(obj: any, nestedString: string, value: any): void {
    const keys = nestedString.split('.');
    const keysLength = keys.length;
    let currentIndex = 0;
    let target = obj;
    while (currentIndex < keysLength - 1) {
        const key = formatKey(keys[currentIndex]);
        if (isUndefined(target[key])) {
            // set(target, key, keyReg.exec(keys[currentIndex + 1]) ? [] : {});
            target[key] = keyReg.exec(keys[currentIndex + 1]) ? [] : {};
        }
        target = target[key];
        currentIndex++;
    }
    const formatCurrentKey = formatKey(keys[currentIndex]);
    // set(target, formatCurrentKey, value);
    target[formatCurrentKey] = value;
}
